﻿using Abp.Auditing;
using GN.Pay.Application.GatewayApp;
using GN.Pay.Application.GatewayApp.Dto.Input;
using GN.Pay.Application.GatewayApp.Dto.Output;
using GN.Pay.GatewayWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GN.Pay.GatewayWeb.Controllers.PayRoute
{
    abstract class APayActionRoute : IPayActionRoute
    {
        protected readonly ITradeApplyAppService service;
        protected readonly IClientInfoProvider clientInfo;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientInfo"></param>
        /// <param name="service"></param>
        protected APayActionRoute(IClientInfoProvider clientInfo, ITradeApplyAppService service)
        {
            this.service = service;
            this.clientInfo = clientInfo;
        }

        /// <summary>
        /// 通道Id
        /// </summary>
        public abstract string ChannelId { get; }

        /// <summary>
        /// 是否支持
        /// </summary>
        public virtual bool IsSupportWap
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 视图结果
        /// </summary>
        /// <param name="model"></param>
        /// <param name=""></param>
        /// <param name="viewResultFactory"></param>
        /// <returns></returns>
        public ActionResult ViewResult(PayChannelModel model, Func<string, object, ActionResult> viewResultFactory)
        {
            if (model == null)
            {
                throw new Abp.UI.UserFriendlyException("申请信息为空");
            }
            if (string.IsNullOrWhiteSpace(model.ApplyId))
            {
                throw new Abp.UI.UserFriendlyException("申请号为空");
            }
            var input = new PayChannelInput()
            {
                AppId = "Cashie",
                ApplyId = model.ApplyId,
                BankCradPayId = model.BankCradPayId,
                MerchantId = "Cashie",
                ChannelId = ChannelId,
                ClientIP = clientInfo.ClientIpAddress == null ? "" : clientInfo.ClientIpAddress,
            };
            var channel = service.PayChannelRegister(input);
            return ViewResult(channel, viewResultFactory);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="output"></param>
        /// <param name="viewResultFactory"></param>
        /// <returns></returns>
        protected abstract ActionResult ViewResult(PayChannelOutput output, Func<string, object, ActionResult> viewResultFactory);

        /// <summary>
        /// 创建支付模型
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="method">方法</param>
        /// <param name="payParams">参数集合</param>
        /// <returns></returns>
        protected PayPageModel CreatePayPageModel(string url, string method, string payParams)
        {
            var model = new PayPageModel() { PayUrl = url, Method = method, Params = new List<KeyValuePair<string, string>>() };
            string[] items = payParams.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in items)
            {
                var index = s.IndexOf("=");
                if (index > 0)
                {
                    string key = s.Substring(0, index).Trim();
                    if (s.Length > index + 1)
                    {
                        model.Params.Add(new KeyValuePair<string, string>(key, s.Substring(index + 1)));
                    }
                }
            }
            return model;
        }
    }
}